package Messages;

import java.io.IOException;
import java.sql.SQLException;
import GUI.Login;
import Logic.LoginInfo;
import ocsf.server.ConnectionToClient;
import Server.SQLConnection;
/**
 * This class implements the Login
 */ 
public class MessageLogin {
	
	private ConnectionToClient client ;
	private String[] str; 
/**
 * Constructor of this class 	
 * @param str
 * @param client
 */
public MessageLogin(String[] str, ConnectionToClient client){
		
		this.client= client ;
		this.str= str; 
	}//end of constractor
/**
 * This method get the role of each user 
 * @throws SQLException
 * @throws IOException
 */
public void Authorization() throws SQLException, IOException{
	 
		SQLConnection conn2= new SQLConnection();
		java.sql.PreparedStatement ps2 = conn2.getConnection().prepareStatement("SELECT u.`idUsers`, u.`Name` ,u.`Password`,u.`EMail`,u.`Role`,u.`Connected` FROM DB.Users u WHERE Name='"+str[1]+"' AND Password='"+str[2]+"';");
		java.sql.ResultSet rs2 = ps2.executeQuery();
			
				if (!rs2.next())
					{		
						client.sendToClient("NotInTheSystem"); // if the user was not found 
						
					}//end of if 
				
				else{
		        	  if(rs2.getString(5).equals("0")) //Manger 
						{
		        			
		        		  
			        		  	if(rs2.getString(6).equals("1")) // the user is already connected to the system
			        		  	{	
			        		  			client.sendToClient("MultipleConnection"); // need to compleate this messag
			                	}
			        		  	
		        		  	else
		        		  	{
		        		  		java.sql.PreparedStatement psl = conn2.getConnection().prepareStatement("Update DB.Users SET Connected=? WHERE idUsers=?");	
			        		  	psl.setInt(1, 1);
			        		  	psl.setString(2, str[1]);
			        		  	psl.executeUpdate();
			        		  
			        			client.sendToClient("Manager");
		        		  	}
					
		        	  
		      	  
		        	  
						}//end of if  
		        	  
		        	  if(rs2.getString(5).equals("1")) //Manger 
						{
		        			
		        		  
			        		  	if(rs2.getString(6).equals("1")) // the user is already connected to the system
			        		  	{	
			        		  			client.sendToClient("MultipleConnection"); // need to compleate this messag
			                	}
			        		  	
		        		  	else
		        		  	{
		        		  		java.sql.PreparedStatement psl = conn2.getConnection().prepareStatement("Update DB.Users SET Connected=? WHERE idUsers=?");	
			        		  	psl.setInt(1, 1);
			        		  	psl.setString(2, str[1]);
			        		  	psl.executeUpdate();
			        		  
			        			client.sendToClient("Market");
		        		  	}
					
		        	  
		      	  
		        	  
						}//end of if  
		        	  
		        	  if(rs2.getString(5).equals("2")) //Manger 
						{
		        			
		        		  
			        		  	if(rs2.getString(6).equals("1")) // the user is already connected to the system
			        		  	{	
			        		  			client.sendToClient("MultipleConnection"); // need to compleate this messag
			                	}
			        		  	
		        		  	else
		        		  	{
		        		  		java.sql.PreparedStatement psl = conn2.getConnection().prepareStatement("Update DB.Users SET Connected=? WHERE idUsers=?");	
			        		  	psl.setInt(1, 1);
			        		  	psl.setString(2, str[1]);
			        		  	psl.executeUpdate();
			        		  
			        			client.sendToClient("installer");
		        		  	}
					
		        	  
		      	  
		        	  
						}//end of if  
				}//end of else 

		}//end of Authorization

}//end of class